<template>
	<view class="content">
		<form class="updateFrom" @submit="updateInfo">
			<view class="">
				<view class="text">
					角色
				</view>
				<view class="roleName from-item">
					<p>
						<input type="text" disabled="true" name="roleName" :value="user.roleName" />
					</p>
				</view>
			</view>

			<view class="">
				<view class="text">
					账号
				</view>
				<view class="username from-item">
					<p>
						<input type="text" disabled="true" name="username" :value="user.username" />
					</p>
				</view>
			</view>

			<view class="">
				<view class="text">
					别名
				</view>
				<view class="nickname from-item">
					<p>
						<input type="text" name="nickname" :value="user.nickname" />
					</p>
				</view>
			</view>
			<view class="">
				<view class="text">
					密码
				</view>
				<view class="password from-item">
					<p>
						<input type="password" name="password" :value="user.password" />
					</p>
				</view>
			</view>
			<view class="">
				<view class="text">
					邮箱
				</view>
				<view class="email from-item">
					<p>
						<input type="text" disabled="true" @confirm="confrimEmail" @blur="confrimEmail" name="email" :value="user.email" />
					</p>
				</view>
			</view>
			<view class="">
				<view  @click="sendEmailConfirm" class="sendEmailConfirm">
					{{sendAuthCode}}
				</view>
				<view class="emailConfirm from-item">
					<p>
						<input type="number"  name="emailConfirm" :value="emailConfirm" />
					</p>
				</view>
			</view>



			<view class="subAndReset">
				<button form-type="submit" type="primary">修改信息</button>
			</view>

		</form>
	</view>
</template>

<script>
	var url = ''
	var globalEmail = ''
	export default {
		data() {
			return {
				user: getApp().globalData.user,
				emailConfirm: '',
				sendAuthCode: '点击发送验证码'
			}
		},
		onShow() {
			
		},
		onLoad() {
			url = this.serverUrl
		},
		methods: {
			updateInfo(param) {
				var username = param.target.value.username;
				var nickname = param.target.value.nickname;
				var password = param.target.value.password;
				var confirmPassword = param.target.value.confirmPassword;
				var email = param.target.value.email;
				var emailConfirm = param.target.value.emailConfirm;

				uni.showModal({
					title: '确认修改',
					success(e) {
						//如果确认才修改
						if (e.confirm) {
							// 调用修改数据的请求
							//后端
							uni.request({
								url: url+'/user/updateInfo',
								method: 'POST',
								data: {
								    emailConfirm: emailConfirm,
								    password: password,
								    nickname: nickname,
								    email: email,
								    username: username    
								},
								header:{
									'token': getApp().globalData.token,
									'content-type': 'application/json'
								},
								success(res) {
									if(res.data.code == '200'){
										uni.showModal({
											title: '重新登录',
											content: '修改成功请重新登录',
											complete() {
												//删除信息.
												getApp().globalData.token = ""
												getApp().globalData.user = {}
												uni.navigateTo({
													url: '../login/login',
												});
											}
										})
									}//修改失败
									else{
										//修改失败
										uni.showModal({
											content: '修改失败，请重试',
											showCancel: false
										})
									}
								}
							})
						}
					}
				})
			},
			//验证邮箱格式
			confrimEmail(param) {
				var email = param.target.value;
				globalEmail = email;
				if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
					return true;
				} else {
					uni.showModal({
						showCancel: false,
						title: '电子邮箱格式不正确'
					})
					return false;
				}

			},
			//发送邮件请求
			sendEmailConfirm(e) {
				//这里我们获取不到this绑定到值，我们只有使用这个的页面的全局变量，scripte里面的globalEmail
				//这里请求后端异步发送邮箱验证码
				uni.request({
					url: this.serverUrl + '/user/emailConfirm',
					method: 'POST',
					data: {
						email: getApp().globalData.user.email
					},
					header: {
						// 使用这个会将data参数自动转化为params
						'content-type': 'application/x-www-form-urlencoded',
					},
					success(res) {
						var code = res.data.code;
						if (code == '200') {
							this.sendAuthCode = '';
							uni.showToast({
								title: '已经发送，请查收'
							})
						} else {
							uni.showToast({
								title: '发送失败，请重试'
							})
						}
					}
				})
			
			}
			//其他方法

		}
	}
</script>

<style>
	.updateFrom {
		margin-top: 200upx;
	}

	.from-item {
		height: 50upx;
		width: 500upx;
		margin-top: 20upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 20upx;
		color: #000000;
		background-color: #FFFFFF;
		text-align: center;
		border-bottom: 1upx solid #000000;
	}
	.sendEmailConfirm{
		color: #005FC5;
	}
	.email , .username , .roleName{
		color: #FF0000;
	}
</style>
